package com.thread;

import com.bean.DocDossierFileCatalog;
import com.bean.DossierFileCatalog;
import com.listener.DownloadListener;
import com.listener.ProgressListener;
import com.util.DBUtil;
import lombok.Data;
import lombok.NonNull;

import java.util.List;

/**
 * @author Smile
 */
@Data
public class FileDownloadThread implements Runnable {
    private String taskID;
    private ProgressListener listener;
    private DBUtil dbUtil;

    private List<DossierFileCatalog> dossierList;
    private List<DocDossierFileCatalog> docDossierList;

    public FileDownloadThread(String taskID, DBUtil dbUtil, @NonNull ProgressListener listener) {
        this.taskID = taskID;
        this.listener = listener;
        this.dbUtil = dbUtil;
    }

    @Override
    public void run() {
        try {
            if (dossierList != null) {
                final int[] dossierCount = {0, 0};
                final int total = dossierList.size();
                for (DossierFileCatalog item : dossierList) {
                    try {
                        item.download(dbUtil, download -> {
                            boolean success = download.isSuccess();
                            String ID = download.getID();
                            if (success) {
                                dossierCount[0]++;
                                System.out.println("电子卷宗" + ID + "下载完成...");
                            } else {
                                dossierCount[1]++;
                                System.out.println("电子卷宗" + ID + "下载失败...");
                            }
                            ProgressListener.Progress progress = new ProgressListener.Progress();
                            progress.setID(ID);
                            progress.setLoaded(dossierCount[0]);
                            progress.setError(dossierCount[1]);
                            progress.setMessage(download.getMessage());
                            progress.setTotal(total);
                            listener.progress(progress);
                        });
                    } catch (Exception e) {

                    }
                }
            }
            if (docDossierList != null) {
                final int[] docDossierCount = {0, 0};
                final int total = docDossierList.size();
                for (DocDossierFileCatalog item : docDossierList) {
                    item.download(dbUtil, download -> {
                        boolean success = download.isSuccess();
                        String ID = download.getID();
                        if (success) {
                            docDossierCount[0]++;
                            System.out.println("文书卷宗" + ID + "下载完成...");
                        } else {
                            docDossierCount[1]++;
                            System.out.println("文书卷宗" + ID + "下载失败...");
                        }
                        ProgressListener.Progress progress = new ProgressListener.Progress();
                        progress.setID(ID);
                        progress.setLoaded(docDossierCount[0]);
                        progress.setError(docDossierCount[1]);
                        progress.setMessage(download.getMessage());
                        progress.setTotal(total);
                        listener.progress(progress);
                    });
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
